/*
 * @Author: CodeHare
 * @Date: 2023-09-19 09:51:03
 * @LastEditors: CodeHare
 * @LastEditTime: 2023-11-02 14:39:37
 * @Description: 站点列表首页按钮组
 */
// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/pages/site/sear_button_group/button_group_model.dart';
import 'package:yt_devops/components/yt_search_button/search_button.dart';
import 'package:yt_devops/pages/site/site_model.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/services/site/site_services.dart';
import 'package:yt_devops/utils/enums.dart';
/// 站点状态
enum SiteStatusType {
  normal(0),
  fault(1),
  all(2);

  final int value;
  const SiteStatusType(this.value);

  // String get label {
  //   switch (value) {
  //     case 0:
  //       return ResKey.normal.tr;
  //     case 1:
  //       return ResKey.alarm.tr;
  //     case 2:
  //       return ResKey.all.tr;
  //
  //     default:
  //       return '';
  //   }
  // }
}
class SearchButtonGroup extends StatefulWidget {
  double? radius;
  Color? shadowColor;
  Color? backgroundColor;
  Function? checkButtonStatus;
  SiteNumModel? siteNumModel;

  SearchButtonGroup({
    Key? key,
    required this.checkButtonStatus,
    required this.siteNumModel,
    this.radius = 12.0,
    this.shadowColor = const Color(0xFFF6F7FB),
    this.backgroundColor = Colors.white,
  }) : super(key: key);

  @override
  State<SearchButtonGroup> createState() => _SearchButtonGroupState();
}

class _SearchButtonGroupState extends State<SearchButtonGroup> {
  SiteServices siteServices = Get.put(SiteServices());
  final ButtonGroupState state = ButtonGroupState();

  /// 按钮控制
  bool _allSelected = true;
  bool _normalSelected = false;
  bool _faultSelected = false;

  void _activeAllButton() {
    setState(() {
      _allSelected = true;
      _normalSelected = false;
      _faultSelected = false;
    });
  }

  void _activeFaultButton() {
    setState(() {
      _allSelected = false;
      _normalSelected = false;
      _faultSelected = true;
    });
  }

  void _activeNormalButton() {
    setState(() {
      _allSelected = false;
      _normalSelected = true;
      _faultSelected = false;
    });
  }

  /// 变更按钮 UI 状态数据
  void updateButtonUI(int status) {
    switch (status) {
      case 0:
        _activeNormalButton();
        break;

      case 1:
        _activeFaultButton();
        break;

      default:
        _activeAllButton();
    }

    widget.checkButtonStatus!(status);
    getButtonsKeywords();
  }

  @override
  void initState() {
    super.initState();
    getButtonsKeywords();
  }

  @override
  void didUpdateWidget(covariant SearchButtonGroup oldWidget) {
    if (oldWidget.siteNumModel != widget.siteNumModel) {
      getButtonsKeywords();
    }
    super.didUpdateWidget(oldWidget);
  }

  Widget _generateButton(index) {
    var keyword = '';
    if(index == 0){
      keyword = ResKey.all.tr;
    }else if(index == 1){
      keyword = ResKey.normal.tr;
    }else if(index == 2){

      keyword = ResKey.alarm.tr;
    }
    // return Container();
    return SearchButton(
      keyword: keyword,
      number: state.buttonsList[index].number!.isEmpty
          ? "0"
          : state.buttonsList[index].number!,
      width: 98.w,
      height:68,
      radius: 8.sp,
      keywordColor: state.buttonsList[index].keywordColor!,
      backgroundColor: state.buttonsList[index].backgroundColor!,
      numberColor: state.buttonsList[index].numberColor!,
      borderColor: state.buttonsList[index].borderColor!,
      status: state.buttonsList[index].status!,
      onClickFatherWidgets: updateButtonUI,
    );
  }

  Widget _generateButtonGroup() {

    Log.d('ScreenUtil().screenWidth==' + ScreenUtil().screenHeight.toString());
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16.w),
      decoration: BoxDecoration(
        color: widget.backgroundColor,
        boxShadow: [
          /// 阴影效果
          BoxShadow(
            offset: Offset(0, 2.h), // 阴影在X轴和Y轴上的偏移
            color: const Color(0x14000000), // 阴影颜色
            blurRadius: 12.sp, // 阴影程度
            spreadRadius: 0, // 阴影扩散的程度 取值可以正数,也可以是负数
          ),
        ],
        borderRadius: BorderRadius.all(Radius.circular(widget.radius!.w)),
      ),
      child: Container(
        height: ScreenUtil().screenHeight >800?92.h:92,
        padding: EdgeInsets.symmetric(vertical: 12.w),

        margin: EdgeInsets.only(left: 12.w,right: 12.w),
        child: ListView.builder(
          padding: EdgeInsets.zero,
          physics: const NeverScrollableScrollPhysics(),
          scrollDirection: Axis.horizontal,
          itemBuilder: (context, index) {
            return Container(
              margin: EdgeInsets.only(right: 12.w),
              child: _generateButton(index),
            );
          },
          itemCount: state.buttonsList.length,
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return _generateButtonGroup();
  }
  /// 数据
  void getButtonsKeywords() async {

    state.buttonsList.clear();
    // await getSitesNum();

    /// 全部
    state.buttonsList.add(
      ButtonsModel(
        keyword: ResKey.all.tr,
        number: widget.siteNumModel!.allNum.toString(),
        keywordColor: const Color(0xFF333333),
        backgroundColor: const Color(0x1A0984F9),
        numberColor: const Color(0xFF333333),
        borderColor:
            _allSelected ? const Color(0xFF0F6EFF) : Colors.transparent,
        status: SiteStatusType.all.value,
      ),
    );

    /// 正常
    state.buttonsList.add(
      ButtonsModel(
        keyword: ResKey.normal.tr,
        number: widget.siteNumModel!.normalNum.toString(),
        keywordColor: const Color(0xFF333333),
        backgroundColor: const Color(0x1A00AE66),
        numberColor: const Color(0xFF00AE66),
        borderColor:
            _normalSelected ? const Color(0xFF00AE66) : Colors.transparent,
        status: SiteStatusType.normal.value,
      ),
    );

    /// 故障
    state.buttonsList.add(
      ButtonsModel(
        keyword: ResKey.alarm.tr,
        number: widget.siteNumModel!.faultNum.toString(),
        keywordColor: const Color(0xFF333333),
        backgroundColor: const Color(0x1AFA3F3F),
        numberColor: const Color(0xFFFA3F3F),
        borderColor:
            _faultSelected ? const Color(0xFFFA3F3F) : Colors.transparent,
        status: SiteStatusType.fault.value,
      ),
    );
  }
}
